//根据数据字典名称,数据字典value，获取数据字典值
function getDicText(dicname, dicValue) {
  var textContent = "";
  var alldic = wx.getStorageSync('Dictionary');
  //根据数据字典名称，获取数据字典值
  var dicfilterbyname = alldic.filter(function(i) {
    return arguments[0].CodeName == dicname;
  });
  var dicVals = dicValue;
  if (dicValue != null && dicValue != undefined) {
    if (typeof(dicValue) != "string") {
      var arrDivValue = "";
      //详情界面，值为[]
      if (dicValue.length > 0) {
        for (x in dicValue) {
          arrDivValue += dicValue[x] + ";";
        }
        if (dicVals.length > 0) {
          dicVals = arrDivValue.substring(0, arrDivValue.length - 1);
        }
      }

    }
    var dicValues = dicVals.split(';');
    if (dicValues != null && dicValues != undefined) {
      if (dicValues.length > 0) {
        for (var i = 0; i < dicValues.length; i++) {
          var dicV = dicValues[i];
          var dicText = dicfilterbyname.filter(function(i) {
            return arguments[0].ItemValue == dicV;
          });
          if (dicText.length > 0) {
            textContent += dicText[0].ItemText + ";";
          }
        }
        if (textContent.length > 0) {
          textContent = textContent.substring(0, textContent.length - 1);
        }
      }
    }
  }
  return textContent;
}

function initGuid() {
  var guid = "";
  for (var i = 1; i <= 32; i++) {
    var n = Math.floor(Math.random() * 16.0).toString(16);
    guid += n;
    if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
      guid += "-";
  }
  return guid;
}

//数组排序
function compare(propertyName) {
  console.log(propertyName)
  return function(object1, object2) {
    var value1 = object1[propertyName];
    var value2 = object2[propertyName];
    if (value2 < value1) {
      return -1;
    } else if (value2 > value1) {
      return 1;
    } else {
      return 0;
    }
  }
}

module.exports = {
  getDicText: getDicText,
  initGuid: initGuid,
  compare: compare
}